//
//  BegainAddressChooseVC.m
//  saler-ios
//
//  Created by 王亚陆 on 17/6/5.
//  Copyright © 2017年 sayimba. All rights reserved.
//

#import "BegainAddressChooseVC.h"

@interface BegainAddressChooseVC ()
<UITableViewDelegate,
UITableViewDataSource
>
{
    UIImageView * nav;
    Common * com;
    UITableView * Table;
    RootViewController * TabBar;
    NSMutableDictionary * dataDic;
    NSMutableDictionary * callBackDic;
    NSMutableArray * tableArrData;
}

@end

@implementation BegainAddressChooseVC

- (void)viewDidLoad {
    [super viewDidLoad];
    nav = [self createNav:@"选择起始地"];
    [self.view addSubview:nav];
    self.view.backgroundColor = UIviewBackColor;
    
    callBackDic = [[NSMutableDictionary alloc]init];
    
    Table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, Main_Width, 86*kHeight) style:UITableViewStylePlain];
    [Table setTableFooterView:[UIView new]];
    Table.delegate = self;
    Table.dataSource = self;
    [self.view addSubview:Table];
    [Table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).with.offset(10*kWidth);
        make.top.mas_equalTo(nav.mas_bottom).with.offset(10*kHeight);
        make.size.mas_equalTo(CGSizeMake(Main_Width-20*kWidth, Main_height-64));
    }];

    [self createDate];

}

-(void)createDate{
    
    NSString *urlString = [NSString stringWithFormat:@"%@",Order_BegainAddress];
    
    [Common requestWithUrlString:urlString contentType:application_json finished:^(id responseObj) {
        
        NSLog(@"%@",responseObj[@"message"]);
        if ([responseObj[@"success"] boolValue]) {
            
            
            tableArrData = responseObj[@"data"];
            if (tableArrData.count >0) {
                [Table reloadData];
            }
        } else {
            
            [WKProgressHUD popMessage:responseObj[@"message"] inView:self.view duration:1.5 animated:YES];
        }
        
    } failed:^(NSString *errorMsg) {
        
        NSLog(@"%@",errorMsg);
        
    }];
}

#pragma mark - 表单协议

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section{
    
    return tableArrData.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString  *CellIdentifier = @"CellIdentifier";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        
//        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    cell.textLabel.text = tableArrData[indexPath.row][@"sname"];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return  CellHeight;
    
}

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    callBackDic = tableArrData[indexPath.row];
    if(_callBackAddress){
        _callBackAddress(callBackDic);
    }

    
    NSLog(@"点击的是：%@",tableArrData[indexPath.row]);
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
